package com.taixu.apikey.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.taixu.common.core.page.TableDataInfo;
import com.taixu.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.taixu.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.taixu.apikey.domain.bo.ApiKeyBo;
import com.taixu.apikey.domain.vo.ApiKeyVo;
import com.taixu.apikey.domain.ApiKey;
import com.taixu.apikey.mapper.ApiKeyMapper;
import com.taixu.apikey.service.IApiKeyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 平台秘钥Service业务层处理
 *
 * @author ximu
 * @date 2023-12-14
 */
@RequiredArgsConstructor
@Service
public class ApiKeyServiceImpl implements IApiKeyService {

    private final ApiKeyMapper baseMapper;

    @Override
    public ApiKeyVo queryByKey(String key) {
        return baseMapper.queryByKey(key);
    }

    /**
     * 查询平台秘钥
     */
    @Override
    public ApiKeyVo queryById(Long keyid){
        return baseMapper.selectVoById(keyid);
    }

    /**
     * 查询平台秘钥列表
     */
    @Override
    public TableDataInfo<ApiKeyVo> queryPageList(ApiKeyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ApiKey> lqw = buildQueryWrapper(bo);
        Page<ApiKeyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询平台秘钥列表
     */
    @Override
    public List<ApiKeyVo> queryList(ApiKeyBo bo) {
        LambdaQueryWrapper<ApiKey> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ApiKey> buildQueryWrapper(ApiKeyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ApiKey> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getKeytext()), ApiKey::getKeytext, bo.getKeytext());
        lqw.eq(bo.getResidue() != null, ApiKey::getResidue, bo.getResidue());
        lqw.eq(bo.getFoundTime() != null, ApiKey::getFoundTime, bo.getFoundTime());
        return lqw;
    }

    /**
     * 新增平台秘钥
     */
    @Override
    public Boolean insertByBo(ApiKeyBo bo) {
        ApiKey add = BeanUtil.toBean(bo, ApiKey.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setKeyid(add.getKeyid());
        }
        return flag;
    }

    /**
     * 修改平台秘钥
     */
    @Override
    public Boolean updateByBo(ApiKeyBo bo) {
        ApiKey update = BeanUtil.toBean(bo, ApiKey.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ApiKey entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除平台秘钥
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
